package com.tom.architect.party5.day38.simple7.base;

import android.os.Bundle;

import com.tom.architect.party5.day38.simple7.inject.InjectPresenter;
import com.tom.architect.party5.day38.simple7.proxy.ActivityMvpProxy;
import com.tom.architect.party5.day38.simple7.proxy.ActivityMvpProxyImpl;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Added by Tom on 2024/09/30.
 * MVP Activity 基类
 */
public abstract class BaseMvpActivity<P extends BasePresenter>
        extends AppCompatActivity
        implements BaseView {
    private P mPresenter;
    private ActivityMvpProxy mMvpProxy;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView();

        // 创建P，创建交给子类，每个Activity不一样。
        mPresenter = createPresenter();
        mPresenter.attach(this);

        // Activity,Fragment,ViewGroup 都可能有，写工具类抽取出去？或者每个地方写一份代码？
        mMvpProxy = createMvpProxy(); // 创建proxy
        mMvpProxy.bindAndCreatePresenter(); // 绑定

        initView();
        initData();
    }

    /**
     * 创建MVP 的代理，参考去实现Fragment，ViewGroup的。
     * @return
     */
    private ActivityMvpProxy createMvpProxy() {
        if (mMvpProxy == null) {
            mMvpProxy = new ActivityMvpProxyImpl<>(this);
        }
        return mMvpProxy;
    }


    // 子类自己实现 P的创建
    protected abstract P createPresenter();

    protected abstract void initData();

    protected abstract void initView();

    protected abstract void setContentView();

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMvpProxy.unbindPresenter(); // 解绑Presenter，实现方法在MvpProxyImpl中。
        mPresenter.detach();
    }

    public P getPresenter() {
        return mPresenter;
    }
}
